package com.example.service.impl;

import com.example.dao.TokenRepository;
import com.example.model.Token;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private TokenRepository tokenRepository;


    @Override
    public Optional<Token> findByEmail(Token token) {
        Optional<Token> t = tokenRepository.findOne(Example.of(token));
        return t;
    }

    @Override
    public Optional<Token> findByToken(Token token) {
        Optional<Token> t = tokenRepository.findOne(Example.of(token));
        return t;
    }

    @Override
    public Token addToken(Token token) {
        if(token.getToken()==null) { // add
            token.setToken("");
            return tokenRepository.save(token);
        } else { //update
            return tokenRepository.save(token);
        }
    }
}
